require "base_def"

local hand_login = {}

--检查服务状态
function hand_login.CheckServiceStatus()       
    local str = string.format("call pr_check_service_status()")
    local ret = CallMysql(str)
    if ret then
        log.info("CheckServiceStatus ret= ", ret)
        local info_str = ret[1] and ret[1][1]
        if info_str and info_str.service_status then
            local okk, service_status = pcall(cjson.decode, info_str.service_status)
            if okk and service_status then
                return true, tonumber(service_status.status)
            end
        end
    else
        log.error("CheckServiceStatus error str= ", str)
    end
end


local function get_game_name_by_id(id)
    local name = TableCopy(g_gameType)
    for k, v in pairs(name or {}) do
        if v == id then
            --客户端的水果机不是sgj这个名字，而是 fruit 需要特殊处理
            if k == "SGJ" then 
                return "fruit" 
            end

            return string.lower(k)
        end
    end
end

--版本列表
function hand_login.ClientGetVersionList(args)
    log.info("hand_login.ClientGetVersionList args= ", args)
    args = args and args or {}
    local procedure = string.format("call pr_backstate_client_get_version_list('%s')", 
        args.user_id    --玩家id有可能是0，表示不存在
    )
    log.info("ClientGetVersionList procedure=", procedure)
    local res = CallMysql(procedure)
    log.info("ClientGetVersionList res= ", res)
    if res==nil then 
        return 
    end

    local back_ret = {}
    for _,v in pairs(res[1] or {}) do
        local temp = {
            gameType = v.type,
            version = v.version_no,
            is_hot_update = v.is_hot_update,
            special = v.special
        }
        if v.type and v.type == 10000 then
            temp.name = "android"
        elseif v.type and v.type == 11000 then
            temp.name = "ios"
        elseif v.type and v.type == 12000 then
            temp.name = "lobby"
        else
            temp.name = get_game_name_by_id(v.type) or "未知游戏类型"..v.type
            --temp.name = "未知游戏类型"
        end
        back_ret[temp.name] = temp
    end

    return true, back_ret
end

--手机号登录或注册,账号登录
function hand_login.UserLogin(args)             
    local str_login = string.format("call pr_login_check(%s,%s)",
        Obj2mstr(args.account), Obj2mstr(args.password)
    )
    local ret = CallMysql(str_login)
    local loginInfo = {}
    if ret and ret[1] and ret[1][1] then 
        local retInfo = ret[1][1]
        log.info("ret=", ret)
        if retInfo.code==501 then
            --账号错误
            loginInfo.err_msg = "账号不存在"
        elseif retInfo.code==502 then
            --密码错误
            loginInfo.err_msg = "密码错误"
        elseif retInfo.code==200 then 
            loginInfo = retInfo
        else 
            log.error("UserLogin error retInfo=", retInfo)
            return 
        end
    else
        log.error("UserLogin error retInfo=", ret)
        return
    end

    log.info("loginInfo=", loginInfo)
    return true, loginInfo
end

--玩家注册
function  hand_login.UserReg(args)
    local nickname = gen_nickname()
    local headimgurl, sex = gen_headimgurl()

    local str_reg = string.format("call pr_login_reg('%s','%s','%s','%s','%s','%s')",
        args.account, crypt.base64encode(args.password), args.channel, nickname, 
        headimgurl, sex 
    )

    local reg_ret
    local ret = CallMysql(str_reg)
    if ret and ret[1] and ret[1][1] then
        reg_ret = ret[1][1]
        if reg_ret.code==501 then
            reg_ret.err_msg = "注册账号已存在"
        elseif reg_ret.code==200 then 
            return true, reg_ret
        else
            reg_ret.err_msg = "注册失败"
            log.error("UserReg error", ret)
        end 
    else
        reg_ret.err_msg = "注册失败"
        log.error("UserReg error", ret)
    end

    log.info("reg_ret=", reg_ret)
    return false, reg_ret
end

--取玩家登录完整信息
function hand_login.GetUserAllInfo(userid)             
    local procedure = string.format("call pr_login_userinfo(%s)",
        Obj2mstr(userid)
    )
    local ret = CallMysql(procedure)
    local userInfo
    if ret and ret[1] and ret[1][1] then 
        userInfo = ret[1][1]
        log.info("userInfo=", userInfo)
        return true, userInfo
    end
end

return  hand_login 